package com.lindroy.controller

import com.lindroy.config.RespResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @author Lin
 * @date 2019/10/14
 * @function 捕获404等异常
 * @Description
 */
@RestController
class ErrorController {

    @Autowired
    private lateinit var errorController: BasicErrorController

    @RequestMapping("/error", method = [RequestMethod.GET, RequestMethod.POST])
    fun error(request: HttpServletRequest, response: HttpServletResponse): RespResult {
        response.status = HttpStatus.OK.value()
        val errorDetail  = errorController.error(request)
        print("错误=$errorDetail")

        return RespResult(errorDetail.statusCodeValue,"error=${errorDetail.body["error"]},message=$${errorDetail.body["message"]}")
    }
}